package com.pcbly.web.components.utils;

import java.util.StringTokenizer;
import java.util.Vector;

import javax.servlet.ServletConfig;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class CodeUtils {
	private static final Log log = LogFactory.getLog(CodeUtils.class);

	public static String getEncoding(ServletConfig config) {
		if (config == null) {
			return null;
		} else {
			String encoding = "";
			encoding = config.getServletContext().getInitParameter("encoding");
			return encoding;
		}
	}

	public static String getUnicode(String str, String token) {
		String strReturn = "";
		try {
			char chs[] = str.toCharArray();
			for (int i = 0; i < chs.length; i++)
				strReturn = String.valueOf(strReturn)
						+ String.valueOf(String.valueOf(chs[i])
								+ String.valueOf(token));

			log.debug(strReturn);
			if (strReturn.equals("20013;20449;")) {
				log.debug("\u9ED8\u8BA4\u7F16\u7801\u662Fgb2312");
				String s = "gb2312";
				return s;
			}
			if (strReturn.equals("214;208;208;197;")) {
				log.debug("\u9ED8\u8BA4\u7F16\u7801\u662Fiso8859-1");
				String s1 = "ISO-8859-1";
				return s1;
			}
			if (strReturn.equals("65533;65533;65533;65533;")) {
				log.debug("\u9ED8\u8BA4\u7F16\u7801\u662Futf-8");
				String s2 = "UTF-8";
				return s2;
			}
		} catch (Exception exception) {
		}
		return strReturn;
	}

	public static String getUnicode(String str) {
		return getUnicode(str, ";");
	}

	public static String getUnicode() {
		return getUnicode("\u4E2D\u4FE1", ";");
	}

	public static String byte2hex(byte b[]) {
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = Integer.toHexString(b[n] & 0xff);
			if (stmp.length() == 1)
				hs = String.valueOf(String
						.valueOf((new StringBuffer(String.valueOf(String
								.valueOf(hs)))).append("0").append(stmp)));
			else
				hs = String.valueOf(hs) + String.valueOf(stmp);
			if (n < b.length - 1)
				hs = String.valueOf(String.valueOf(hs)).concat(":");
		}

		return hs.toUpperCase();
	}

	protected static String byte2hex_old(byte b[]) {
		String strReturn = "";
		for (int i = 0; i < b.length; i++)
			strReturn = String.valueOf(strReturn)
					+ String.valueOf(String.valueOf(String.valueOf(b[i]))
							.concat(":"));

		return strReturn;
	}

	protected static byte[] hex2byte_old(String str) {
		Vector<String> vecTmp = new Vector<String>();
		log.debug("str is :".concat(String.valueOf(String.valueOf(str))));
		vecTmp = splitString(str, ":");
		byte bytReturn[] = new byte[vecTmp.size()];
		for (int i = 0; i < vecTmp.size(); i++)
			bytReturn[i] = Byte.parseByte(vecTmp.get(i).toString());

		return bytReturn;
	}

	public static byte[] hex2byte(String str) {
		Vector<String> vecTmp = new Vector<String>();
		vecTmp = splitString(str, ":");
		byte bytReturn[] = new byte[vecTmp.size()];
		for (int i = 0; i < vecTmp.size(); i++) {
			String stmp = vecTmp.get(i).toString();
			byte btmp = (byte) (Integer.parseInt(stmp, 16) | 0xffffff00);
			bytReturn[i] = btmp;
		}

		return bytReturn;
	}

	public static Vector<String> splitString(String strTemp, String token) {
		Vector<String> vtTemp = new Vector<String>();
		StringTokenizer stkEmpCode = new StringTokenizer(strTemp, token);
		int j = 0;
		for (; stkEmpCode.hasMoreElements(); vtTemp.add(stkEmpCode.nextToken()))
			j++;

		return vtTemp;
	}
}
